import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';

class ItemModelData with ChangeNotifier {
  final String id;

  int zOrder = 0;

  Offset position;

  Size size;

  Size minSize;

  final String? type;

  String? parentId;

  final List<String> childrenIds = [];

  bool isSelected;

  final dynamic userData;

  ItemModelData({
    String? id,
    this.position = Offset.zero,
    this.size = const Size(80, 80),
    this.minSize = const Size(10, 10),
    this.type,
    this.isSelected = false,
    this.userData,
  })  : assert(minSize < size),
        this.id = id ?? Uuid().v4();

  updateItem() {
    notifyListeners();
  }

  move(Offset offset) {
    this.position += offset;
    notifyListeners();
  }

  setPosition(Offset position) {
    this.position = position;
    notifyListeners();
  }

  resizeDelta(Offset deltaSize) {
    var tmpSize = size + deltaSize;
    if (tmpSize.width < minSize.width) {
      tmpSize = Size(minSize.width, tmpSize.height);
    }

    if (tmpSize.height < minSize.height) {
      tmpSize = Size(minSize.width, minSize.height);
    }
    size = tmpSize;
    notifyListeners();
  }

  setSize(Size size) {
    this.size = size;
    notifyListeners();
  }

  Offset getOffsetOnItem(Alignment alignment) {
    return Offset(
      size.width * ((alignment.x + 1) / 2),
      size.height * ((alignment.y + 1) / 2),
    );
  }

  setParent(String parentId) {
    this.parentId = parentId;
  }

  removeParent() {
    this.parentId = null;
  }

  addChild(String childId) {
    childrenIds.add(childId);
  }

  removeChild(String childId) {
    childrenIds.remove(childId);
  }

  @override
  String toString() {
    return 'Model Data ($id), position: $position';
  }
}
